<template>
	<view class="wdthdrawal">
		<view class="pagetitle">
			<uni-breadcrumb separator="/">
				<uni-breadcrumb-item v-for="(route,index) in routes" :key="index" :to="route.to">
					{{route.name}}
				</uni-breadcrumb-item>
			</uni-breadcrumb>
		</view>
		<view class="uni-container" style="width:100%;">
			<uni-forms ref="form" :model="formData" validate-trigger="submit" label-width="120px" err-show-type="toast"   >
				
				<uni-forms-item name="shop_id" label="店铺id">
					<uni-easyinput v-model="formData.shop_id" :disabled="true" placeholder="关联shop表" ></uni-easyinput>
				</uni-forms-item>
				
				<uni-forms-item name="user_id" label="用户id">
					<uni-easyinput v-model="formData.user_id"  :disabled="true" placeholder="关联uni-id-users表" ></uni-easyinput>
				</uni-forms-item>
				<uni-forms-item name="legal_person_name" label="法人姓名">
					<uni-easyinput v-model="formData.legal_person_name"></uni-easyinput>
				</uni-forms-item>
				<uni-forms-item name="legal_person_card" label="法人身份证号码">
					<uni-easyinput v-model="formData.legal_person_card"></uni-easyinput>
				</uni-forms-item>
				<uni-forms-item name="legal_person_card_photo_z" label="法人身份证正面">
					<uni-file-picker return-type="object" v-model="formData.legal_person_card_photo_z"></uni-file-picker>
				</uni-forms-item>
				<uni-forms-item name="legal_person_card_photo_f" label="法人身份证反面">
					<uni-file-picker return-type="object" v-model="formData.legal_person_card_photo_f"></uni-file-picker>
				</uni-forms-item>
				<uni-forms-item name="business_license_title" label="营业执照注册名称">
					<uni-easyinput v-model="formData.business_license_title"></uni-easyinput>
				</uni-forms-item>
				<uni-forms-item name="business_license_number" label="社会统一编号">
					<uni-easyinput v-model="formData.business_license_number" placeholder="营业执照注册编号，组织机构代码号" ></uni-easyinput>
				</uni-forms-item>
				<uni-forms-item name="business_license_address" label="营业执照注册地址">
					<uni-easyinput v-model="formData.business_license_address"></uni-easyinput>
				</uni-forms-item>
				
				<uni-forms-item name="business_license_photo" label="营业执照照片">
					<uni-file-picker return-type="object" v-model="formData.business_license_photo"></uni-file-picker>
				</uni-forms-item>
				
				<uni-forms-item name="permit_photo" label="许可证照片">
					<uni-file-picker return-type="object" v-model="formData.permit_photo"></uni-file-picker>
				</uni-forms-item>
				
				<uni-forms-item name="audit_status" label="审核状态">
					<switch @change="binddata('audit_status', $event.detail.value)" :checked="formData.audit_status">
					</switch>
					<view style="font-size:25rpx;color:#999;line-height: 60rpx;">
						false代表审核未通过，true代表审核通过
					</view>
				</uni-forms-item>
				
				<uni-forms-item name="audit_text" label="审核说明文字">
					<uni-easyinput v-model="formData.audit_text" type="textarea" placeholder="失败说明原因，成功直接显示：审核通过" ></uni-easyinput>
				</uni-forms-item>
				<view class="uni-button-group">
					<button type="primary" class="uni-button" @click="submit">提交</button>
					<button type="" class="uni-button" style="width:130px;margin:0 50px" @click="urlBack">返回</button>
				</view>
			</uni-forms>
		</view>
	</view>
</template>

<script>
	import {
		validator
	} from '@/js_sdk/validator/shop_authentication.js';

	const db = uniCloud.database();
	const dbCollectionName = 'shop_authentication';

	function getValidator(fields) {
		let result = {}
		for (let key in validator) {
			if (fields.indexOf(key) > -1) {
				result[key] = validator[key]
			}
		}
		return result
	}
	import Util from "@/js_sdk/validator/util.js"
	const global = uniCloud.importObject("global", {
		customUI: true
	})
	let dayDate;
	export default {
		data() {
			let formData = {
				"shop_id": "",
				"user_id": "",
				"legal_person_name": "",
				"legal_person_card": "",
				"legal_person_card_photo_z": null,
				"legal_person_card_photo_f": null,
				"business_license_title": "",
				"business_license_number": "",
				"business_license_address": "",
				"business_license_photo": null,
				"permit_photo": null,
				"create_date": null,
				"audit_status": false,
				"audit_text": ""
			}
			return {
				routes: [{
						to: "/pages/index/index",
						name: "首页",
					},
					{
						to: "/pages/shop/shop/shop-info",
						name: "商铺信息",
					},
					{
						to: "",
						name: "商铺详情",
					},
					{
						to: "",
						name: "编辑营业资质"
					}
				],
				formData,
				formOptions: {},
				rules: {
					...getValidator(Object.keys(formData))
				}
			}
		},
		onLoad(e) {
			if (e._uid) {
				const id = e._uid
				this.formDataId = id
				this.getDetail(id)
			}else{
				uni.showModal({
					title:'参数错误',
					showCancel:false,
					content:'当前环境不正确，有异常风险，为了平台安全将强制跳转至首页，请您稍后重试'
				}).then(res=>{
					if (res.confirm) {
						uni.reLaunch({
							url:'/'
						})
					}
				})
			}
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			/**
			 * 验证表单并提交
			 */
			submit() {
				uni.showLoading({
					mask: true
				})
				this.$refs.form.validate().then((res) => {
					return this.submitForm(res)
				}).catch(() => {}).finally(() => {
					uni.hideLoading()
				})
			},

			/**
			 * 提交表单
			 */
			submitForm(value) {
				// 使用 clientDB 提交数据
				return db.collection(dbCollectionName).doc(this.formDataId).update(value).then((res) => {
					uni.showToast({
						icon: 'none',
						title: '修改成功'
					})
					this.getOpenerEventChannel().emit('refreshData')
					setTimeout(() => uni.navigateBack(), 500)
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				})
			},
			/**
			 * 获取表单数据
			 * @param {Object} id
			 */
			getDetail(id) {
				uni.showLoading({
					mask: true
				})
				db.collection(dbCollectionName).doc(id).field(
					"shop_id,user_id,legal_person_name,legal_person_card,legal_person_card_photo_z,legal_person_card_photo_f,business_license_title,business_license_number,business_license_address,business_license_photo,permit_photo,create_date,audit_status,audit_text"
				).get().then((res) => {
					const data = res.result.data[0]
					if (data) {
						this.formData = data

					}
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				}).finally(() => {
					uni.hideLoading()
				})
			},
			urlBack(){
				uni.navigateBack(1)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.select {
		width: 500rpx;
		margin: 0 20rpx;
	}

	.pagetitle {
		padding: 10rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;

		.right {
			display: flex;
		}
	}

	.warn {
		padding: 10rpx;
	}

	.item {
		padding: 10rpx;
		margin: 0rpx;
		border-bottom: 1rpx solid #eee;
		display: flex;
		justify-content: space-between;
		align-items: center;

		.items {
			flex: 1;
			text-align: center;
		}
	}

	.title {
		font-size: 16px;
		font-weight: bold;
	}

	.table {
		margin: 50rpx 20rpx;
		border: 1px solid #eee;
		border-bottom: none
	}

	.wdthdrawal {
		padding: 20rpx;
		font-size: 14px;
	}
</style>
<style>
	.uni-container {
		padding: 15px;
	}

	.uni-input-border,
	.uni-textarea-border {
		width: 100%;
		font-size: 14px;
		color: #666;
		border: 1px #e5e5e5 solid;
		border-radius: 5px;
		box-sizing: border-box;
	}

	.uni-input-border {
		padding: 0 10px;
		height: 35px;

	}

	.uni-textarea-border {
		padding: 10px;
		height: 80px;
	}

	.uni-button-group {
		margin-top: 50px;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		justify-content: center;
	}

	.uni-button {
		width: 204px;
	}
</style>